package com.pointstar.dp.modules.project.service.impl;

import java.util.List;
import java.util.Map;

import com.pointstar.dp.modules.project.util.PinyinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pointstar.dp.common.entity.Page;
import com.pointstar.dp.common.entity.Query;
import com.pointstar.dp.common.entity.R;
import com.pointstar.dp.common.utils.CommonUtils;
import com.pointstar.dp.modules.project.entity.ProductEntity;
import com.pointstar.dp.modules.project.dao.ProductMapper;
import com.pointstar.dp.modules.project.service.ProductService;

/**
 * 商品表
 * @author Korey<m13057531285@163.com>
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {

	private final ProductMapper productMapper;

	@Autowired
	public ProductServiceImpl(ProductMapper productMapper) {
		this.productMapper = productMapper;
	}

	/**
	 * 分页查询
	 * @param params
	 * @return
	 */
	@Override
	public Page<ProductEntity> listProduct(Map<String, Object> params) {
		Query query = new Query(params);
		Page<ProductEntity> page = new Page<>(query);
		productMapper.listForPage(page, query);
		return page;
	}

	/**
	 * 查询
	 * @return
	 */
	@Override
	public List<ProductEntity> listProductNoPage() {
		return productMapper.list();
	}

    /**
     * 新增
     * @param product
     * @return
     */
	@Override
	public R saveProduct(ProductEntity product) {
		product.setPyCode(PinyinUtils.converterToFirstSpell(product.getName()));
		int count = productMapper.save(product);
		return CommonUtils.msg(count);
	}

    /**
     * 根据id查询
     * @param id
     * @return
     */
	@Override
	public R getProductById(Integer id) {
		ProductEntity product = productMapper.getObjectById(id);
		return CommonUtils.msg(product);
	}

    /**
     * 修改
     * @param product
     * @return
     */
	@Override
	public R updateProduct(ProductEntity product) {
		int count = productMapper.update(product);
		return CommonUtils.msg(count);
	}

    /**
     * 删除
     * @param id
     * @return
     */
	@Override
	public R batchRemove(Integer[] id) {
		int count = productMapper.batchRemove(id);
		return CommonUtils.msg(id, count);
	}

}
